package impl

import (
	"github.com/gin-gonic/gin"
	"github.com/starmotioncf/common/region"
	"github.com/starmotioncf/soutchatinternal/dto"
	"github.com/starmotioncf/soutchatinternal/model"
	"github.com/starmotioncf/soutchatinternal/repository"
	"github.com/starmotioncf/soutchatinternal/service"
)

type countryService struct {
	*service.Service
	countryRepo repository.CountryRepository
}

func NewCountryService(s *service.Service, countryRepo repository.CountryRepository) service.CountryService {
	return &countryService{
		Service:     s,
		countryRepo: countryRepo,
	}
}

// GetList 获取国家列表,rgn=0时返回所有
func (s *countryService) GetList(ctx *gin.Context, rgn region.Region) (list []*dto.CountryItem, err error) {
	var res []*model.Country
	if rgn == 0 {
		res, err = s.countryRepo.GetAll(ctx)
	} else {
		res, err = s.countryRepo.GetByRegion(ctx, rgn)
	}
	list = make([]*dto.CountryItem, 0, len(res))
	if err != nil {
		return
	}
	for i := range res {
		list = append(list, &dto.CountryItem{
			Code: res[i].Code,
			Name: res[i].Name,
			Icon: res[i].Icon,
		})
	}
	return
}
